package com.chatplus.application.common.util;


import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil extends StringUtils {

    private static final Pattern linePattern = Pattern.compile("_(\\w)");

    public static String underlineToCamelCase(String str) {
        return underlineToCamelCase(str, false);
    }

    public static String underlineToCamelCase(String str, boolean firstUp) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase(Locale.ROOT));
        }
        matcher.appendTail(sb);
        decideFirstLetterCase(sb, firstUp);
        return sb.toString();
    }

    private static void decideFirstLetterCase(StringBuffer sb, boolean fristUp) {
        if (fristUp) {
            String fristWord = sb.substring(0, 1).toUpperCase(Locale.ROOT);
            sb.replace(0, 1, fristWord);
        } else {
            String fristWord = sb.substring(0, 1).toLowerCase(Locale.ROOT);
            sb.replace(0, 1, fristWord);
        }
    }

    /**
     * 驼峰转分隔字符串
     * eg: AbcDef -> abc-def
     *
     * @param str       驼峰字符串
     * @param delimiter 分隔符
     * @return
     */
    public static String unCamelWithDelimiter(String str, String delimiter) {
        if (StrUtil.isEmpty(str)) {
            return StrUtil.EMPTY;
        }
        if (StrUtil.isEmpty(delimiter)) {
            delimiter = StrUtil.DASHED;
        }
        String s = str.replaceAll("(?=[A-Z]+)", delimiter).trim().toLowerCase(Locale.ROOT);
        if (s.startsWith(delimiter)) {
            s = s.substring(1);
        }
        return s;
    }

    public static String unCamel(String str) {
        if (StrUtil.isEmpty(str)) {
            return StrUtil.EMPTY;
        }
        String delimiter = StrUtil.DASHED;
        return unCamelWithDelimiter(str, delimiter);
    }

    /**
     * dash字符串转小驼峰
     * eg: abc-def -> abcDef
     *
     * @param value dash字符串
     * @return 小驼峰字符串
     */
    public static String dashToCamelCase(String value, boolean firstUp) {
        StringBuffer sb = new StringBuffer(value.length());
        boolean upper = false;

        for (char c : value.toCharArray()) {
            if (c == '-') {
                upper = true;
                continue;
            }
            if (upper) {
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(c);
            }
            upper = false;
        }
        decideFirstLetterCase(sb, firstUp);
        return sb.toString();
    }

    public static String dashToCamelCase(String value) {
        return dashToCamelCase(value, false);
    }

    public static boolean allEmpty(String... strings) {
        for (String string : strings) {
            if (StrUtil.isNotBlank(string)) {
                return false;
            }
        }
        return true;
    }
    /**
     *  判断字符串是否 是数组里任意一个开头的
     * @param str 原始字符串
     * @param prefixes 比较的开头数组
     * @return 返回 数组里命中的字符串或者原始字符串
     */
    public static String startWithAny(String str, List<String> prefixes) {
        if (CharSequenceUtil.isEmpty(str) || ArrayUtil.isEmpty(prefixes)) {
            return str;
        }
        for (String suffix : prefixes) {
            if (CharSequenceUtil.startWith(str, suffix, false)) {
                return suffix;
            }
        }
        return str;
    }

    /**
     * 去掉字符串的最后一个字符
     * @param str 原始字符串
     * @param replaceChar 要去掉的最后一个字符
     * @return 返回去掉后的字符串
     */
    public static String replaceLastCharacter(String str, char replaceChar) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        int lastIndex = str.length() - 1;
        if (str.charAt(lastIndex) == replaceChar) {
            return str.substring(0, lastIndex);
        } else {
            return str;
        }
    }
}
